/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.model.Product;
import com.minierp.model.Stock;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class InBillTemplateExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"InBillTemplate.xls\"");
		HSSFCellStyle cellStyleBold = workbook.createCellStyle();
		HSSFFont boldFont = workbook.createFont();
		boldFont.setBoldweight(700);
		cellStyleBold.setFont(boldFont);
		cellStyleBold.setBorderLeft(1);
		cellStyleBold.setBorderRight(1);
		cellStyleBold.setBorderTop(1);
		cellStyleBold.setBorderBottom(1);
		HSSFSheet sheet = workbook.createSheet("list");
		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		c = r.createCell(0);
		c.setCellStyle(cellStyleBold);
		c.setCellValue(new HSSFRichTextString("产品编号"));
		c = r.createCell(1);
		c.setCellStyle(cellStyleBold);
		c.setCellValue(new HSSFRichTextString("名称"));
		c = r.createCell(2);
		c.setCellStyle(cellStyleBold);
		c.setCellValue(new HSSFRichTextString("单位"));
		c = r.createCell(3);
		c.setCellStyle(cellStyleBold);
		c.setCellValue(new HSSFRichTextString("数量"));
		c = r.createCell(4);
		c.setCellStyle(cellStyleBold);
		c.setCellValue(new HSSFRichTextString("单价"));
		c = r.createCell(5);
		c.setCellStyle(cellStyleBold);
		c.setCellValue(new HSSFRichTextString("备注"));
		List list = (List) model.get("list");
		int index = 1;

		for (int os = 0; os < list.size(); ++os) {
			Product p = (Product) ((Object[]) list.get(os))[0];
			Stock s = (Stock) ((Object[]) list.get(os))[1];
			if (s.getAmount().compareTo(BigDecimal.ZERO) == 1) {
				BigDecimal unitPrice = s.getCost().divide(s.getAmount(), 4, 4);
				r = sheet.createRow(index++);
				r.createCell(0).setCellValue(
						new HSSFRichTextString(p.getProductCode()));
				r.createCell(1).setCellValue(
						new HSSFRichTextString(p.getProductName()));
				r.createCell(2).setCellValue(
						new HSSFRichTextString(p.getUnit()));
				r.createCell(3).setCellValue(s.getAmount().doubleValue());
				r.createCell(4).setCellValue(unitPrice.doubleValue());
				r.createCell(5).setCellValue(new HSSFRichTextString(""));
			}
		}

		sheet.setColumnWidth(0, 3000);
		sheet.setColumnWidth(1, 5000);
		sheet.setColumnWidth(2, 3000);
		sheet.setColumnWidth(3, 3000);
		sheet.setColumnWidth(4, 3000);
		sheet.setColumnWidth(5, 5000);
		ServletOutputStream arg15 = response.getOutputStream();
		workbook.write(arg15);
		arg15.flush();
		arg15.close();
	}
}